define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kuerp/staff_sign_in/index' + location.search,
                    add_url: 'kuerp/staff_sign_in/add',
                    edit_url: 'kuerp/staff_sign_in/edit',
                    del_url: 'kuerp/staff_sign_in/del',
                    multi_url: 'kuerp/staff_sign_in/multi',
                    import_url: 'kuerp/staff_sign_in/import',
                    table: 'kuerp_staff_sign_in',
                }
            });

            var table = $("#table");
            var staff_id = Config.staff_id;
            var edit_auth = Config.edit_auth;
            var issupper = Config.issupper;
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id'), operate: false},
                        {field: 'customers.customer_name', title: __('Customer_id'), operate: 'LIKE',  align:'left',formatter: function(value, row, index){
                            return "<a href='javascript:void(0);' style='color: #4397fd' class='btn-dialog' " +
                                    "data-url='kuerp/customers/detail/ids/"+row.customer_id+"' data-title='客户详情' data-area=[\"80%\",\"80%\"] >" + row.customers.customer_name + "</a>";
                        }},
                        {field: 'relation_process', title: __('Relation_process'), operate: 'LIKE'},
                        {field: 'location', title: __('Location'), operate: 'LIKE', table: table, class: 'autocontent', formatter: function(value, row, index) {
                            var html = row.location
                            if (row.lng) {
                                html = '<a href="https://apis.map.qq.com/uri/v1/marker?marker=coord:'+row.lat+','+row.lng+';title:'+row.location+';addr:'+row.location+'&referer=crm" target="_blank" >'+row.location+'</a>'
                            }
				            return html
                        }},
                        {field: 'staffmember.name', title: __('Staff_id'), operate: 'LIKE', formatter: Table.api.formatter.search},
                        {field: 'next_time', title: __('Next_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field     : 'operate',
                            title     : __('Operate'),
                            table     : table,
                            events    : Table.api.events.operate,
                            formatter : Table.api.formatter.operate,
                            buttons   : [
                                {
                                    name      : '详情',
                                    text      : __('详情'),
                                    classname: 'btn btn-xs btn-info  btn-dialog',
                                    icon: 'fa fa-file-o',
                                    url       : 'kuerp/staff_sign_in/detail',
                                    callback: function (data) {
                                        $('.btn-success').trigger('click');
                                    },

                                },
                                {
                                    name: 'edit',
                                    text: __(''),
                                    classname: 'btn btn-xs btn-success  btn-dialog',
                                    icon: 'fa fa-pencil',
                                    url: function (row, column) { //row 表格接收到的数据
                                        return "kuerp/staff_sign_in/edit?ids=" + row.id; //弹窗的对应后台控制器方法 这里是默认index方法 加id参数
                                    },
                                    visible:function(row){
                                        return (row.staff_id == staff_id && edit_auth) || issupper;
                                    },
                                    // extend: 'data-area=\'["90%","90%"]\'',
                                    refresh: true
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'kuerp/staff_sign_in/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'kuerp/staff_sign_in/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'kuerp/staff_sign_in/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
